package com.shujia.kafka

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object Demo1KafkaProducer {
  def main(args: Array[String]): Unit = {

    /**
      * 1、创建生产者
      *
      */

    val properties = new Properties

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //生产者对象
    val producer = new KafkaProducer[String, String](properties)


    //构建一行数据，指定topic
    val record = new ProducerRecord[String, String]("words", "spark")

    //将数据发送到kafka
    producer.send(record)

    //关闭链接
    producer.close()

  }

}
